package controller;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import data.Data;

@SuppressWarnings("serial")
public class ActiveSitemapServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		HttpSession session = request.getSession();
		DBObject dbObject = (DBObject) session.getAttribute("users");
		if(dbObject==null)
		{
			try {
				response.sendRedirect("/");
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		String sitemap=request.getParameter("sitemap");
		String work = (String)session.getAttribute("work");
		
		if(work==null)
		{
			try {
				response.sendRedirect("/manager-blog");
				return;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String status = request.getParameter("status");
		DBCollection collection = Data.data("urlCraw");
		DBObject object=collection.findOne(new BasicDBObject("username",dbObject.get("username")).append("blogurl", work).append("sitemap", sitemap));
		if(object != null)
		{
			object.put("status", status);
			collection.save(object);
			response.sendRedirect("/list-source");
			return;
		} else {
			response.sendRedirect("/list-source");
			return;
		}
		
	}
}
